<?php
namespace app\index\model;
use think\Db;
use think\Model;
use think\Session;
use think\Cookie;

class Common extends Model
{
    /// 获取正在生成的报告数量
    public  function  getReportCreateNow(){
        $data = Db::query('select count(1) as sum  from Lis_ReportTasks where ProcessFlag =0');
        foreach ($data as $count){
            return $count['sum'];
        }
    }

    /// 重新生成报告单  根据条码  -修改状态
    public function  ModifyReportStatus($ybid){
        /*if(strlen($ybid) !=12){
            return false;
        }*/
        $res = Db::table('Lis_ReportTasks')->where('Barcode',$ybid)
            ->update(['ProcessFlag' => 0,'MergeFlag' => 9999]);
        if ($res){
            return true;
        }
        return false;
    }

    //  重新生成报告  根据yqdh , ybbh, cdrq
    public function ModifyReportStatusByYYC($cdrq,$ybbh,$yqdh){
        $res = Db::table('lis_ybxx')->where(['cdrq'=>$cdrq,'ybbh'=>$ybbh,'yqdh'=>$yqdh])->find();
        if($res){
            $cc = $this->ModifyReportStatus($res['ybid']);
            if($cc){
                return true;
            }
        }
        return false;
    }


    /***
     * 重新生成报告单  根据医院代号  时间 -修改状态
     * TestDate -> cdrq
     * taskdata -> bgrq
     * @param $ybid
     * @return bool
     */
    public function  ModifyReportStatusByYydhCdrq($yydh, $TestDate){
        if(strlen($yydh) !=7){
            return false;
        }
        return $res = Db::execute('UPDATE dbo.Lis_ReportTasks SET ProcessFlag =0 WHERE Barcode like :Barcode and TestDate > :TestDate',
            ['Barcode'=>$yydh, 'TestDate'=>$TestDate]);
        if ($res){
            return true;
        }
        return false;
    }

    //批量重新生成报告单  /*修改状态 array('66336363','2233433')*/
        public  function ModifyReportStatusAll($string){
        if(empty($string)){
            return false;
        }
        return $result = Db::execute('UPDATE dbo.Lis_ReportTasks SET ProcessFlag =0 WHERE Barcode in (:Barcode)', ['Barcode'=>$string]);

    }

       /* return $res = Db::table('Lis_ReportTasks')
            ->where('Barcode','in',$string)
            ->update(['ProcessFlag' => 0]);*/
     /*   return $res = Db::execute('update Lis_ReportTasks set ProcessFlag = :ProcessFlag where Barcode in (:Barcode)',['ProcessFlag'=>0,'Barcode'=>]);
    }*/

    //判断复制用户是工号还是姓名
    public  function IdOrUsername($str){
        if(preg_match("/[\x7f-\xff]/", $str)){
            return true;
            //含有汉字
        }else{
            return false;
            //不是汉字
        }
    }


    /***
     * 判断各个专业组 当天的标本量  $yqdh ,$cdrq
     * 返回当天的 检验个数 和 审核个数 和 总数
     */
    public  function GetReportCountByCY($yqdh,$cdrq){
        $j = Db::table('lis_ybxx')
            ->where(['yqdh'=>$yqdh,'cdrq'=>$cdrq,'ybzt'=>'j'])
            ->count();

        $s = Db::table('lis_ybxx')
            ->where(['yqdh'=>$yqdh,'cdrq'=>$cdrq,'ybzt'=>'s'])
            ->count();
        return array($j,$s,$all=$j+$s);
    }


    /***
     * 第二种方法
     * 判断各个专业组 当天的标本量  $yqdh ,$cdrq
     * 返回当天的 检验个数 和 审核个数 和 总数
     */
    public  function GetReportCountByRq($cdrq){
        $j = Db::query("SELECT yqdh,COUNT(*) AS Count FROM lis_Ybxx WHERE ybzt = 'j' AND cdrq = :cdrq GROUP BY yqdh,ybzt ",['cdrq'=>$cdrq]);

        $s = Db::query("SELECT yqdh,COUNT(*) AS Count FROM lis_Ybxx WHERE ybzt = 's' AND cdrq = :cdrq GROUP BY yqdh,ybzt ",['cdrq'=>$cdrq]);

        $sum = Db::query("SELECT yqdh,COUNT(*) AS Count FROM lis_Ybxx WHERE  cdrq = :cdrq GROUP BY yqdh,ybzt ORDER BY COUNT DESC",['cdrq'=>$cdrq]);

        return array('j'=>$j,'s'=>$s,'sum'=>$sum);
    }

    /***
     * 返回所有在用专业组当前时间的标本统计
     */
    public function GetReportCountAll($cdrq){

        foreach ($this->GetbusiYq() as $item =>$value){
            $j = Db::table('lis_ybxx')
                ->where(['yqdh'=>$value['yqdh'],'cdrq'=>$cdrq,'ybzt'=>'j'])
                ->count();

            $s = Db::table('lis_ybxx')
                ->where(['yqdh'=>$value['yqdh'],'cdrq'=>$cdrq,'ybzt'=>'s'])
                ->count();
            $count = array_push($end=array(),array($value['yqdh'] => array($j,$s)));
        }
        return $count;
    }

    /***
     * 返回正在可用的仪器表
     */
    public  function GetbusiYq(){
        return $Yq = Db::table('xt_yqb')
                   ->field(['yqdh','yqmc'])
                   ->where('isfs',1)
                   ->select();
    }
    /***
     * 返回专业组 的仪器代号
     */
    public function GetYqdh(){
        return $res = Db::table('xt_yqb')
            ->query("SELECT yqdh,yqmc FROM dbo.xt_yqb where isuse = 1");
    }

    /***
     * 返回专业组 的代号
     */
    public function GetGcode(){
        return $res = Db::table('t_base_labgroup')
            ->query("SELECT gcode,gname FROM dbo.t_base_labgroup order by cast(gcode as int) ");
    }
    /***
     * 返回外送的委托单位
     */
    public function GetOuthosiptal(){
        return $res = Db::table('f_outhosiptal')
            ->query("select id,hosiptalname from f_outhosiptal where zt = '1' order by id");
    }


    /***
     * 返回当前系统设置的标本种类
     */
     public function GetBbzl($dh=''){
         if(empty($dh)) {
             return $res = Db::table('xt_bbzl')
                 ->query("SELECT dh,mc FROM dbo.xt_bbzl ORDER BY CAST(dh AS INT) ASC");
         }
         $res = Db::table('xt_bbzl')->field('mc')->where('dh',$dh)->find();
         return $res;
     }


    /***
     * 返回当前系统设置的标本种类  返回数字
     */
    public function GetBbzlNumber($str=''){
           $res = Db::table('xt_bbzl')->field('mc','dh')->where('mc',$str)->find();
            return $res['dh'];
    }
    /***
     * 替换新的条形码
     */
    public function ReplaceCode($precode,$newcode){
         $res = Db::query('EXEC dbo.sp_ks_EditYbid(:preybid,:curybid)',['preybid' => $precode,'curybid' =>$newcode]);
         var_dump($res);exit();
    }

    /***
     * 判断当前的用户是否是管理员 是的 返回支持  不是的 返回权限不允许
     */

    public function IsAdminUser(){
        $logid = Session::get('userinfo.logid');
        $usercookie = Cookie::get('userinfo');
        if($logid == 'lis' || !empty($usercookie)){
            return true;
        }
           return  false;
    }
    function FileCount($dir){
        global $count;
        if(is_dir($dir)&&file_exists($dir)){
            $ob=scandir($dir);
            foreach($ob as $file){
                if($file=="."||$file==".."){
                    continue;
                }
                $file=$dir."/".$file;
                if(is_file($file)){
                    $count++;
                }elseif(is_dir($file)){
                    FileCount($file);
                }
            }
        }
    }



}